package org.rsbot.script.randoms;

import org.rsbot.script.Random;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.wrappers.RSInterfaceChild;
import org.rsbot.script.wrappers.RSItemTile;
import org.rsbot.script.wrappers.RSNPC;
import org.rsbot.script.wrappers.RSObject;
import org.rsbot.script.wrappers.RSTile;

/**
 * @author Secret Spy
 * @version 2.4 - 02/24/11
 */
@ScriptManifest(authors = "Secret Spy", name = "ScapeRuneIsland", version = 2.4)
public class ScapeRuneIsland extends Random {

    public int[] StatueIDs = {8992, 8993, 8990, 8991};
    public RSTile CenterTile = new RSTile(3421, 4777);
    public RSObject Direction;
    public boolean Finished;
    public boolean Fishing;
    public boolean ForceTalk;

    public boolean activateCondition() {
        return distanceTo(CenterTile) < 50;
    }

    @Override
    public void onFinish() {
        Direction = null;
        Finished = false;
        Fishing = false;
    }

    public int getBoxCount(int... ids) {
        if (!getInterface(11).isValid()) {
            return -1;
        }
        int count = 0;
        for (int i = 0; i < 28; ++i) {
            for (int id : ids) {
                if (getInterface(11).getComponent(17).isValid()
                        && getInterface(11).getComponent(17).getComponentID() == id) {
                    count++;
                }
            }
        }
        return count;
    }

    public int loop() {
        setCameraAltitude(true);
        if (!activateCondition()) {
            return -1;
        }
        RSObject Statue1 = getNearestObjectByID(StatueIDs[0]);
        RSObject Statue2 = getNearestObjectByID(StatueIDs[1]);
        RSObject Statue3 = getNearestObjectByID(StatueIDs[2]);
        RSObject Statue4 = getNearestObjectByID(StatueIDs[3]);
        if (getMyPlayer().isMoving() || getMyPlayer().getAnimation() == 620) {
            return random(550, 700);
        }
        if (getInterface(241).getComponent(4).isValid() && getInterface(241).getComponent(4).getText().contains("catnap")) {
            Finished = true;
        }
        if (getInterface(64).getComponent(4).isValid() && getInterface(64).getComponent(4).getText().contains("fallen asleep")) {
            Finished = true;
        }
        if (getInterface(242).getComponent(4).isValid() && getInterface(242).getComponent(4).getText().contains("Wait! Before")) {
            ForceTalk = true;
        }
        if (canContinue()) {
            if (clickContinue()) {
                return random(500, 1000);
            }
        }
        if (ForceTalk) {
            RSNPC Servant = getNearestNPCByID(2481);
            if (Servant != null && Direction == null && getSetting(344) == 0) {
                if (!tileOnScreen(Servant.getLocation())) {
                    walkTileMiniM(getClosestTileOnMap(Servant.getLocation()));
                    return 700;
                }
                if (atNPC(Servant, "Talk-to")) {
                    ForceTalk = false;
                }
                return random(1000, 2000);
            }
            if (Servant == null) {
                Servant = getNearestNPCByID(2481);
                if (Servant == null) {
                    walkTileMiniM(getClosestTileOnMap(CenterTile));
                    return random(1000, 2000);
                }
                return random(50, 100);
            }
        }
        if (Finished) {
            RSObject Portal = getNearestObjectByID(8987);
            if (Portal != null) {
                if (!tileOnScreen(Portal.getLocation())) {
                    walkTileMiniM(getClosestTileOnMap(Portal.getLocation()));
                    return random(500, 1000);
                } else {
                    if (atObject(Portal, "Enter")) {
                        return random(3000, 5000);
                    }
                    return random(500, 1000);
                }
            } else {
                walkTileMiniM(getClosestTileOnMap(CenterTile));
            }
        }
        if (getInterface(11).isValid() && getBoxCount() - getBoxCount(6209, 6202, 6200) >= 27) {
            RSInterfaceChild RandomItem = getInterface(11, (random(16, 26)));
            int RandomID = RandomItem.getComponentID();
            if (RandomID < 0) {
                return random(50, 100);
            }
            log("Item with ID " + RandomID + " was deposited.");
            if (atInterface(11, (random(16, 26)), "Dep")) {
                return random(500, 1000);
            }
            return random(50, 100);
        }
        if (getInterface(11).isValid() && getBoxCount() - getBoxCount(6209, 6202, 6200) < 27) {
            bank.close();
            return random(500, 1000);
        }
        if (getInventoryCountExcept(6209, 6202, 6200) >= 27) {
            RSObject Box = getNearestObjectByID(32930);
            if (!tileOnScreen(Box.getLocation())) {
                walkTileMiniM(getClosestTileOnMap(Box.getLocation()));
                return random(1000, 2000);
            } else {
                log("Depositing item(s) to make room.");
                if (atObject(Box, "Deposit")) {
                    wait(random(1800, 2000));
                    clickMouse(410 + random(2, 4), 235 + random(2, 1), false);
                    atMenu("Deposit-1");
                    wait(random(1200, 1400));
                    clickMouse(435 + random(2, 4), 40 + random(2, 1), true);
                    return random(800, 1200);
                }
                return random(500, 1000);
            }
        }
        if (getInventoryCount(6202) > 0) {
            final RSObject Pot = getNearestObjectByID(8985);
            if (Pot != null) {
                if (!tileOnScreen(Pot.getLocation())) {
                    walkTileMiniM(getClosestTileOnMap(Pot.getLocation()));
                    return random(400, 800);
                }
                atInventoryItem(6202, "Use");
                sleep(random(800, 1000));
                if (atObject(Pot, "Use")) {
                    sleep(1000);
                }
                return random(2000, 2400);
            } else {
                walkTileMiniM(getClosestTileOnMap(CenterTile));
            }
        }
        if (Fishing && getInventoryCount(6209) == 0) {
            final RSItemTile Net = getNearestGroundItemByID(6209);
            if (Net != null) {
                if (!tileOnScreen(Net)) {
                    walkTileMiniM(getClosestTileOnMap(Net));
                    return random(800, 1000);
                } else {
                    atTile(Net, "Take");
                    return random(800, 1000);
                }
            } else {
                walkTileMiniM(getClosestTileOnMap(CenterTile));
            }
        }
        if (getInterface(246).getComponent(5).containsText("contains")
                && getSetting(334) == 1 && Direction == null) {
            sleep(2000);
            if (tileOnScreen(Statue1.getLocation())) {
                Direction = Statue1;
                Fishing = true;
            }
            if (tileOnScreen(Statue2.getLocation())) {
                Direction = Statue2;
                Fishing = true;
            }
            if (tileOnScreen(Statue3.getLocation())) {
                Direction = Statue3;
                Fishing = true;
            }
            if (tileOnScreen(Statue4.getLocation())) {
                Direction = Statue4;
                Fishing = true;
            }
            log("Checking direction");
            return random(2000, 3000);
        }
        if (Direction != null && getInventoryCount(6200) < 1) {
            wait(random(1000, 1200));
            if (!tileOnScreen(Direction.getLocation())) {
                walkTileMiniM(getClosestTileOnMap(Direction.getLocation()));
                return random(400, 600);
            }
            final RSObject Spot = getNearestObjectByID(8986);
            if (Spot != null) {
                if (!tileOnScreen(Spot.getLocation())) {
                    turnToTile(Spot.getLocation());
                }
                if (!tileOnScreen(Spot.getLocation())
                        && walkTileMiniM(Spot.getLocation())) {
                    sleep(random(1000, 2000));
                    if (!tileOnScreen(Spot.getLocation())) {
                        sleep(1000);
                    }
                }
                atTile(Spot.getLocation(), "Net");
                return random(2000, 2500);
            } else {
                walkTileMiniM(getClosestTileOnMap(CenterTile));
            }
        }
        if (getInventoryCount(6200) > 0) {
            final RSNPC Cat = getNearestNPCByID(2479);
            if (Cat != null) {
                if (!tileOnScreen(Cat.getLocation())) {
                    walkTileMiniM(getClosestTileOnMap(Cat.getLocation()));
                }
                atInventoryItem(6200, "Use");
                sleep(random(500, 1000));
                atNPC(Cat, "Use Raw fish-like thing -> Evil bob");
            } else {
                walkTileMiniM(getClosestTileOnMap(CenterTile));
            }
            return random(1900, 2200);
        }
        RSNPC Servant = getNearestNPCByID(2481);
        if (Servant != null && Direction == null && getSetting(344) == 0) {
            if (!tileOnScreen(Servant.getLocation())) {
                walkTileMiniM(getClosestTileOnMap(Servant.getLocation()));
                return 700;
            }
            atNPC(Servant, "Talk-to");
            return random(1000, 2000);
        }
        if (Servant == null) {
            Servant = getNearestNPCByID(2481);
            if (Servant == null) {
                walkTileMiniM(getClosestTileOnMap(CenterTile));
                return random(1000, 2000);
            }
            return random(50, 100);
        }
        log("Setting 344: " + getSetting(344));
        return random(800, 1200);
    }
}
